var app = getApp();
var util = require("../../utils/util.js");
var zhdomain = app.globalData.zhdomain;
Page({
  data: {
    banner:[], //轮播图
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    stories:[], //列表
    page:0, //页数
    date:[],
    len:0,
    lens:[]
  },
  onLoad: function(options) {
    var that = this;
    wx.request({
        url: zhdomain+'/api/4/news/latest', 
        header: {
            'content-type': 'application/json'
        },
        success: function (res) {
          var data = res.data;
          that.setData({
            banner:data.top_stories,
            stories:data.stories,
            len:data.stories.length,
          })
          that.data.lens[that.data.page] = data.stories.length;
        },
        complete:function(res){
          if(res.statusCode != 200){
            wx.showModal({
              content: '加载失败请重试[errorCode:'+res.statusCode+']',
              showCancel: false
            });
          }
        }
    })
  },
  onReachBottom:function(){
    var that = this;
    var beforeDate = util.setDayVal(-that.data.page).format('YYYY-MM-DD').replace(/-/gi,"");
    let date = beforeDate.substring(4,6)+"月"+beforeDate.toString().substring(6,8)+"日";
    wx.request({
        url: zhdomain+'/api/4/news/before/'+beforeDate, 
        header: {
            'content-type': 'application/json'
        },
        success: function (res) {
          var data = res.data;
          that.data.lens[that.data.page+1] = that.data.lens[that.data.page] + data.stories.length;
          let lens = that.data.lens;
          that.setData({
            len: that.data.len+data.stories.length,
            stories: that.data.stories.concat(data.stories),
            page: that.data.page+1,
            date: that.data.date.concat(date),
            lens: lens
          })
          //设置标题
          /*wx.setNavigationBarTitle({
            title: date
          })*/
        },
        complete:function(res){
          if(res.statusCode != 200){
            wx.showModal({
              content: '加载失败请重试[errorCode:'+res.statusCode+']',
              showCancel: false
            });
          }
        }
    })
  }
})